package com.springcloud.commons.base;

import com.springcloud.commons.result.Result;
import com.springcloud.commons.utils.StringEscapeEditor;
import com.springcloud.commons.utils.StringUtils;
//import com.springcloud.jwt.JwtConfigProperty;
//import com.springcloud.jwt.JwtUtil;
import com.springcloud.model.SysUser;
import com.springcloud.service.ISysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description：基础 controller
 * @author：wangpeng
 * @date：2015/10/1 14:51
 */
public abstract class BaseController {
    // 控制器本来就是单例，这样似乎更加合理
    protected Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private ISysUserService userService;
//    @Autowired
//    private JwtConfigProperty jwtConfig;
//    @Autowired
//    private JwtUtil jwtUtil;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor());
    }

    /**
     * 获取当前登录用户对象
     * @return
     */
    public SysUser getCurrentUser(Long userid) {

        SysUser user = userService.selectById(userid);
        return user;
    }

    /**
     * 获取当前登录用户id
     * @return
     */
//    public Long getUserId(HttpServletRequest request) {
//        String authHeader = "";
//        Cookie[] cookies = request.getCookies();
//        if (cookies!=null) {
//            for (int i = 0; i < cookies.length; i++) {
//                Cookie cookie = cookies[i];
//                if (cookie.getName().equals(jwtConfig.getHeader())) {
//                    authHeader = cookie.getValue();
//                    break;
//                }
//            }
//        }
//        if(StringUtils.isBlank(authHeader)) {
//            authHeader = request.getHeader(jwtConfig.getHeader());//获取浏览器传过来的token信息
//        }
//        String tokenHead = jwtConfig.getTokenHead(); //获取配置文件的token开头
//        String authToken = authHeader.substring(tokenHead.length());//去掉开头
//        String userid = jwtUtil.getSubjectFromToken(authToken); // 获取userid
//        return Long.parseLong(userid);
//    }


    /**
     * ajax失败
     * @param msg 失败的消息
     * @return {Object}
     */
    public Object renderError(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }


    /**
     * ajax成功
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    /**
     * ajax成功
     * @param obj 成功时的对象
     * @return {Object}
     */
    public Object renderSuccess(Object obj) {
        Result result = new Result();
        result.setData(obj);
        return result;
    }
}
